package cn.limeizi.shoppingcar.daos.impl;

import cn.limeizi.shoppingcar.MyTools;
import cn.limeizi.shoppingcar.beans.McBean;
import cn.limeizi.shoppingcar.beans.OrderItemBean;
import cn.limeizi.shoppingcar.daos.McDao;
import cn.limeizi.shoppingcar.daos.OrderItemDao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 李美姿
 * @Date: 2019/7/17 15:16
 * @Version 1.0
 * @描述：
 */
public class OrderItemDaoImpl implements OrderItemDao {

    private Connection connection;//数据库连接对象
    private PreparedStatement preparedStatement;//预编译对象
    private ResultSet resultSet;//查询接收对象

    /*
     * 初始化连接
     * */
    //新的独立连接
    public OrderItemDaoImpl() {
        connection = MyTools.getConn();
    }

    //来自外部连接
    public OrderItemDaoImpl(Connection connection) {
        this.connection = connection;
    }


    @Override
    public int addOrderItem(OrderItemBean orderItemBean) {
        connection = MyTools.getConn();
        int out = 0;
        try {
            String sql = "insert into shoppingcar(mcId,userId,mcNumber) values(?,?,?)";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,orderItemBean.getMcId());
            preparedStatement.setInt(2,orderItemBean.getUserId());
            preparedStatement.setInt(3,orderItemBean.getMcNumber());
            out = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            MyTools.closeMysql(connection, preparedStatement, resultSet);//关闭数据库
        }
        return out;
    }

    @Override
    public int deleteOrderItem(int itemId) {
        connection = MyTools.getConn();
        int out = 0;
        try {
            String sql = "delete from shoppingcar where itemId=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,itemId);
            out = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            MyTools.closeMysql(connection, preparedStatement, resultSet);//关闭数据库
        }
        return out;
    }

    @Override
    public int updateOrderItem(OrderItemBean orderItemBean) {
        connection = MyTools.getConn();
        int out = 0;
        try {
            String sql = "update shoppingcar set mcId=?,userId=?,mcNumber=? where itemId=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,orderItemBean.getMcId());
            preparedStatement.setInt(2,orderItemBean.getUserId());
            preparedStatement.setInt(3,orderItemBean.getMcNumber());
            preparedStatement.setInt(4,orderItemBean.getItemId());
            out = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            MyTools.closeMysql(connection, preparedStatement, resultSet);//关闭数据库
        }
        return out;
    }

    @Override
    public int clear(int userId) {
        connection = MyTools.getConn();
        int out = 0;
        try {
            String sql = "delete from shoppingcar";
            preparedStatement = connection.prepareStatement(sql);
            out = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            MyTools.closeMysql(connection, preparedStatement, resultSet);//关闭数据库
        }
        return out;
    }

    @Override
    public List<OrderItemBean> getAll(int userId) {
        connection=MyTools.getConn();
        List<OrderItemBean> orderItemBeanList=new ArrayList<>();
        try {
            String sql="select * from shoppingcar where userId=?";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setInt(1,userId);
            ResultSet resultSet=preparedStatement.executeQuery();
            while (resultSet.next()){
                OrderItemBean orderItemBean=new OrderItemBean();
                orderItemBean.setItemId(resultSet.getInt("itemId"));
                orderItemBean.setMcId(resultSet.getInt("mcId"));
                orderItemBean.setUserId(resultSet.getInt("userId"));
                orderItemBean.setMcNumber(resultSet.getInt("mcNumber"));
                orderItemBeanList.add(orderItemBean);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            MyTools.closeMysql(connection, preparedStatement, resultSet);//关闭数据库
        }
        return orderItemBeanList;
    }

    @Override
    public OrderItemBean getOne(int userId, int mcId) {
        connection=MyTools.getConn();
        try {
            String sql="select * from shoppingcar where mcId=?";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setInt(1,mcId);
            ResultSet resultSet=preparedStatement.executeQuery();
            while (resultSet.next()){
                OrderItemBean orderItemBean=new OrderItemBean();
                orderItemBean.setItemId(resultSet.getInt("itemId"));
                orderItemBean.setUserId(resultSet.getInt("userId"));
                orderItemBean.setMcNumber(resultSet.getInt("mcNumber"));
                orderItemBean.setMcId(resultSet.getInt("mcId"));
                return orderItemBean;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            MyTools.closeMysql(connection, preparedStatement, resultSet);//关闭数据库
        }
        return null;
    }
}